package com.ly.jdk8.lambda;
import	java.util.Comparator;
import java.util.List;
import java.util.function.BiPredicate;
import	java.util.function.Supplier;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import	java.util.function.Consumer;

/**
 * @Classname MethodRef
 * @Description
 * 三种方法引用
 *  对象：：实例方法名
 *  类：：实例方法名
 *  类：：静态方法名
 * @Date 2020/9/27 15:40
 * @Author 冷心影翼
 */
public class MethodRef {

    public static void main(String[] args) {
        ref1();
        ref2();
        ref3();
        ref4();
    }

    public static void ref1() {
        PrintStream out = System.out;
        Consumer consumer = out::println;
        Arrays.asList(1,2,3,4,5).forEach(consumer);
        Arrays.asList(1,2,3,4,5).forEach(System.out::println);
    }

    public static void ref2() {
        Comparator<Integer> comparator = Integer::compare;
        System.out.println(comparator.compare(1, 3));
    }

    public static void ref3() {
        BiPredicate<String,String> bi = (x,y)-> x.equals(y);
        //若前者调用方法的参数是后者，可以使用 类：：静态方法名
        BiPredicate<String,String> bi2 = String::equals;
        System.out.println(bi2.test("1.txt", "!2"));
    }

    //构造器引用
    public static void ref4() {
       Supplier<List> supplier = ()-> new ArrayList();
       Supplier<List> supplier2 = ArrayList::new;
        List list = supplier2.get();
        list.add(1);
        System.out.println(list);
    }
}
